<!--  -->
<template>
  <div class="image-list-container">
    <div class="action-head">
      <el-radio-group
        v-model="collect"
        @change="onChangeImage"
        size="mini"
      >
        <el-radio-button :label="false">全部</el-radio-button>
        <!-- <el-radio-button :label="true">收藏</el-radio-button> -->
      </el-radio-group>
      <el-button
        size="mini"
        type="success"
        @click="dialogUploadVisible=true"
        v-if="isShowUpload"
      >上传素材</el-button>
    </div>
    <!-- 素材列表 -->
    <el-row
      :gutter="20"
      class="image-list"
    >
      <el-col
        :xs="12"
        :sm="6"
        :md="4"
        v-for="(item,index) in images"
        :key="index"
        class="image-item"
        @click.native="selected=index"
        style="position:relative"
      >
        <!-- <el-image style="height: 100px"
                  :src="'http://1.15.144.141:8081/uploads/'+item.img"
                  fit="cover"></el-image> -->
        <el-image
          style="height: 98%; position: absolute; transform: translate(-50%, -50%); left:50%; top: 50%; width:98%"
          :src="baseURL+'/uploads/'+item.img"
          fit="contain"
        ></el-image>
        <div
          class="selected"
          v-if="isShowSelected && selected===index"
        ></div>
      </el-col>
    </el-row>
    <!-- 数据分页 -->
    <el-dialog
      title="上传素材"
      :visible.sync="dialogUploadVisible"
      :append-to-body="true"
    >
      <!-- <el-upload class="upload-demo"
                 drag
                 action="http://api-toutiao-web.itheima.net/mp/v1_0/user/images"
                 :headers="uploadHeaders"
                 name="image"
                 :on-success="onUploadSuccess"
                 :show-file-list="false"
                 multiple>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip"
             slot="tip">只能上传jpg/png文件，且不超过500kb</div>
      </el-upload> -->
      <!-- :headers="uploadHeaders" -->
      <el-upload
        class="upload-demo"
        drag
        :auto-upload="false"
        name="image"
        action=""
        :on-change="onChange"
        :show-file-list="false"
        multiple
      >
        <!-- 组件内部发请求，不是axios要单独配置 -->
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div
          class="el-upload__tip"
          slot="tip"
        >只能上传jpg/png文件，且不超过500kb</div>
      </el-upload>
    </el-dialog>
  </div>
</template>

<script>
import { getImages, uploadImages } from '@/api/images'
import readFileImg from '@/utils/image-compress' // 图片压缩
export default {
  name: '',
  components: {},
  props: {
    isShowUpload: {
      type: Boolean,
      default: true
    },
    isShowSelected: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      baseURL: this.baseURL,
      collect: false,
      images: [],
      dialogUploadVisible: false,
      // uploadHeaders: {
      //   Authorization: `Bearer ${user.token}`
      // }, // 单独配置上传图片请求
      selected: null
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loadImages(false)
  },
  mounted () { },
  methods: {
    async loadImages (collect = false) {
      const res = await getImages({ collect })
      this.images = res.data.data
    },
    onChangeImage (value) {
      this.loadImages(value)
    },
    async onChange (file, fileList) {
      const fd = await new Promise((resolve, reject) => {
        readFileImg(file.raw, resolve, reject)
      })
      uploadImages(fd).then(res => {
        this.loadImages(false)
        this.dialogUploadVisible = false
      })
    }
  }
}
</script>

<style lang='less' scoped>
.action-head {
  display: flex;
  justify-content: space-between;
}
.image-list {
  margin: 20px auto;
  padding: 0 10px;
}
.image-item {
  width: 150px;
  height: 120px;
  margin: 0 5px 5px 0;
  border: 2px solid #a7a7a7;
  cursor: pointer;
  position: relative;
}
.selected {
  background: url(../../../assets/selected.png) no-repeat;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  background-size: cover;
  opacity: 0.8;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
